//创建一个weindow的常用对象
class wd {
	constructor() {
		this.wd = window;
		this.Dom = this.wd.document;
		this.sess = this.wd.sessionStorage;
		this.local = this.wd.localStorage;
		this.GetUerl = this.wd.location;
		this.xhr = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP");
	}
}

class Ajax extends wd {
	/*Content-type (几种常见的Content-Type类型)
            application/x-www-form-urlencoded
            multipart/form-data
            application/json
            text/xml
			application/json;charset=UTF-8
            */
	constructor(url, data, way = 'post', header = 'application/json;charset=UTF-8', datatype = 'JSON', async = true) {
		super();

		this.ArrList = {
			url,
			data,
			way,
			async,
			header,
			datatype
		}
	}
	post() {
		return new Promise((resolve, reject) => {
			let xhr = this.xhr;
			let {
				url,
				data,
				way,
				async,
				header,
				datatype
			} = this.ArrList;
			xhr.open(way, url, async);
			xhr.datatype = datatype;
			xhr.setRequestHeader('Content-type', `${header}`);
			if (xhr && xhr.timeout) {
				xhr.timeout = 30000;
				xhr.ontimeout = function() { /*请求超时触发的事件*/
					alert('请求超时!')
				};
			}
			xhr.send(JSON.stringify(data));
			xhr.onerror = function(e) { /*失败*/
				reject(new Error({
					errorType: 'abort_error',
					xhr: xhr
				}));
			};
			xhr.onreadystatechange = function() {
				if (xhr.status === 200 || xhr.status === 304 || xhr.status === 400) {
					resolve(JSON.parse(xhr.responseText))
				}
				// console.log(`http=>状态码:${xhr.readyState}`)
				reject(`http=>状态码:${xhr.readyState}`)
			}

		})

	}
}


class session extends wd { // 封装sessionStorage 继承 wd 方法
	constructor(sess) { // 默认为sessionStorage 传入参数为localStorage 
		super(); //超类
		this.storage = sess === undefined ? this.sess : this.local;
	}
	set(key, value) {
		this.storage.setItem(this.wd.btoa(this.wd.encodeURIComponent(key)), this.wd.btoa(this.wd.encodeURIComponent(value)));
	}
	get(key) {
		return this.wd.decodeURIComponent(this.wd.atob(this.storage.getItem(this.wd.btoa(key))))
	}
	removeKey(key) {
		key && this.storage.removeItem(this.wd.decodeURIComponent(this.wd.btoa(key)));
	}
	removeAll() {
		this.storage.clear();
	}
}

class getDate {
	constructor() {
		this.date = new Date();
		this.time = this.date.getTime() //时间戳
		this.getDay = this.date.getDay();
		this.DayTime = 24 * 60 * 60 * 1000;
	}
	getseven() { //周一
		var MondayTime = this.time - (this.getDay - 1) * this.DayTime // 显示周一
		function p(s) {
			return s < 10 ? '0' + s : s
		};
		const d = new Date(MondayTime)
		const begintime = String(d.getFullYear()) + '-' + String(p((d.getMonth() + 1))) + '-' + String(p(d.getDate()));
		return begintime;
	}
	getSunday() { //周末
		let MondayTime = this.time - (this.getDay - 7) * this.DayTime // 显示周一
		function p(s) {
			return s < 10 ? '0' + s : s
		};
		const d = new Date(MondayTime)
		const begintime = String(d.getFullYear()) + '-' + String(p((d.getMonth() + 1))) + '-' + String(p(d.getDate()));
		return begintime;
	}
	getmonthsta() { //本月第一天
		this.date.setDate(1)
		let month = String(this.date.getMonth() + 1)
		let day = this.date.getDate()
		if (month <= 9) {
			month = '0' + month
		}
		if (day <= 9) {
			day = '0' + day
		}
		return String(this.date.getFullYear()) + '-' + String(month) + '-' + String(day);
	}
	getmonthend() { //本月最后一天
		let currentMonth = this.date.getMonth()
		let nextMonth = ++currentMonth
		let nextMonthFirstDay = new Date(this.date.getFullYear(), nextMonth, 1)
		let oneDay = 1000 * 60 * 60 * 24
		let lastTime = new Date(nextMonthFirstDay - oneDay)
		let month = parseInt(lastTime.getMonth() + 1)
		let day = lastTime.getDate()
		let yue;

		// if (month < 10) {
		//   month = '0' + month
		// }
		month = month < 10 ? '0' + month : "";
		yue = day < 10 ? '0' + day : "";
		// if (day < 10) {
		//   day = '0' + day
		// }
		let date = new Date(String(this.date.getFullYear()) + '-' + String(month) + '-' + String(day))
		if ((date.getMonth() + 1) <= 9) {
			yue = '0' + (date.getMonth() + 1)
		} else {
			yue = (date.getMonth() + 1)
		}
		let dateValue = String(date.getFullYear()) + '-' + String(yue) + '-' + String(date.getDate());
		return dateValue
	}
}

/**
 * 计算经纬度差值
 * @param {我当前经度} lat1
 * @param {我当前纬度} lng1
 * @param {企业的经度} lat2
 * @param {企业的纬度} lng2
 */
function positionToCalculate(lat1, lng1, lat2, lng2) {
	function Rad(d) {
		return (d * Math.PI) / 180.0 // 经纬度转换成三角函数中度分表形式。
	}
	let radLat1 = Rad(lat1)
	let radLat2 = Rad(lat2)
	let a = radLat1 - radLat2
	let b = Rad(lng1) - Rad(lng2)
	let s = 2 * Math.asin(
		Math.sqrt(
			Math.pow(Math.sin(a / 2), 2) +
			Math.cos(radLat1) *
			Math.cos(radLat2) *
			Math.pow(Math.sin(b / 2), 2)))
	s = s * 6378.137 // EARTH_RADIUS;
	s = Math.round(s * 1000) / 1000 // 这里输出米   //输出为公里为 1Math.round(s * 10000) / 10000;
	let result = s.toString().split('.')
	// 位置完全相同
	if (result == 0) {
		return result[0]
	} else if (result[0] != 0) { // 当前距离大于一公里
		return null
	}
	return result[1] // 返回当前距离的米
};
/**
 * 数组对象去重
 * @param {Object} obj
 */
function deteleObject(obj) {
	var uniques = []
	var stringify = {}
	for (var i = 0; i < obj.length; i++) {
		var keys = Object.keys(obj[i])
		keys.sort(function(a, b) {
			return (Number(a) - Number(b))
		})
		var str = ''
		for (var j = 0; j < keys.length; j++) {
			str += JSON.stringify(keys[j])
			str += JSON.stringify(obj[i][keys[j]])
		}
		if (!stringify.hasOwnProperty(str)) {
			uniques.push(obj[i])
			stringify[str] = true
		}
	}
	return uniques
};

function positionToCalculate(lat1, lng1, lat2, lng2) {
	function Rad(d) {
		return (d * Math.PI) / 180.0 // 经纬度转换成三角函数中度分表形式。
	}
	let radLat1 = Rad(lat1)
	let radLat2 = Rad(lat2)
	let a = radLat1 - radLat2
	let b = Rad(lng1) - Rad(lng2)
	let s = 2 * Math.asin(
		Math.sqrt(
			Math.pow(Math.sin(a / 2), 2) +
			Math.cos(radLat1) *
			Math.cos(radLat2) *
			Math.pow(Math.sin(b / 2), 2)))
	s = s * 6378.137 // EARTH_RADIUS;
	s = Math.round(s * 1000) / 1000 // 这里输出米   //输出为公里为 1Math.round(s * 10000) / 10000;
	let result = s.toString().split('.')
	// 位置完全相同
	if (result == 0) {
		return result[0]
	} else if (result[0] != 0) { // 当前距离大于一公里
		return null
	}
	return result[1] // 返回当前距离的米
};

/**
 * 用来返回当前枚举的
 * @param {数组} arr
 * @param {值} value
 */
function transition(arr, value) {
	for (let s in arr) {
		if (arr[s].value == value) {
			return arr[s].label
		}
	}
}

/**
 * 传入数组返回出现次数最对的值
 * @param {arr} arrlist
 */

function much(arrlist) {
	const d = {}
	arrlist.forEach(k => !d[k] ? d[k] = 1 : d[k]++)
	const max = Object.keys(d).sort((a, b) => d[b] - d[a])[0]
	return max
};
/**
 * 获取当前设备类型
 * 不传入阐述就返回PC端 || 移动端
 * 传入参数为移动端设备类型
 */
function terminalEquipment(type = null) {
	let userAgentInfo = navigator.userAgent
	let Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod']
	if (type) {
		for (let v = 0; v < Agents.length; v++) {
			if (userAgentInfo.indexOf(Agents[v]) > 0) {
				var flag = Agents[v]
				break
			}
		}
		return flag
	} else {
		let flag = 'PC端'
		for (let v = 0; v < Agents.length; v++) {
			if (userAgentInfo.indexOf(Agents[v]) > 0) {
				flag = '移动端'
				break
			}
		}
		return flag
	}
}

class FindDom extends wd {
	constructor() {
		super();
	}
	
	setElement(label = 'div') { //创建标签
		return this.Dom.createElement(label)
	}
	/*查找DOM start */
	/**
	 * @param {Object} id
	 * @param {string} Node father(父级) son(子级) noe (第一个) end(最后一个) upwith(同级前元素) withnext(同级后元素) sonCont(返回子节点个数)
	 */
	getId(id,Node){
		console.warn(id,Node)
		let d = this.Dom.getElementById(id)
		if(id && Node === undefined) {
			return d
		}
		if(id && Node === 'father') { //获取某一个节点的父级元素
			return d.parentNode
		}
		if(id && Node === 'son') { //返回最后一个节点
			return d.lastElementChild
		}
		if(id && Node === 'noe') { //返回某个原数第一个节点
		
			return d.firstElementChild
		}
		if(id && Node === 'end') { //返回某个原数第一个节点
			
			return d.firstChild;
		}
		if (id && Node === 'upwith') { //指向前一个同辈元素；previousSibling的元素版
			return d.previousElementSibling
		}
		if (id && Node === 'withnext') { //指向最后一个同辈元素；nextSibling的元素版。
			return d.nextElementSibling
		}
		if (id && Node === 'sonCont') { //返回子元素（不包括文本节点和注释）的个数
			return d.childElementCount;
		}
		
	}
	getClass(name){
		return this.Dom.getElementsByClassName(name)
	}
	getName(name){
		return this.Dom.getElementsByTagName(name)
	}
	getAll(All){
		return this.Dom.querySelectorAll(All)
	}
	getSelector(leba){
		return this.Dom.querySelector(leba);
	}
	/*查找DOM end */
	
}


class extends wd {
	constructor() {
	    
	}
}